﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Collections;

/// <summary>
/// Summary description for CareerPath
/// </summary>

namespace Joseph.CareerPathApplication
{
    public class CareerPath
    {
        public CareerPath()
        {
            //
            // TODO: Add constructor logic here
            //
        }


        public string RootPath
        {
            get
            {
                return System.Web.HttpContext.Current.Server.MapPath("~/Applications");

            }
        }

        public IEnumerable<cp_Application> NewGetJobApplications()
        {
            CareerPathDataContext db = new CareerPathDataContext();
            var query = from a in db.cp_Applications
                        select new cp_Application
                        {
                            Date = a.Date,
                            PositionName = a.PositionName,
                            SourceID = a.SourceID,
                            CompanyName = a.CompanyName,
                            CompanyEMail = a.CompanyEMail,
                            City = a.City,
                            Notes = a.Notes,
                            AdPath = RootPath + a.AdPath,
                            FilePath = RootPath + a.FilePath

                        };
            return query.ToList();
        }

        public IEnumerable<cp_Application> GetJobApplications()
        {
            CareerPathDataContext ca = new CareerPathDataContext();
            IEnumerable<cp_Application> joeApps = from c in ca.cp_Applications 
                                                  select c;
            return joeApps;
        }

        public IEnumerable<cp_Application> GetJobApplications(int ApplicationID)
        {
            CareerPathDataContext ca = new CareerPathDataContext();
            IEnumerable<cp_Application> joeApps = from c in ca.cp_Applications 
                                                  where c.ApplicationID == ApplicationID
                                                  select c;
            return joeApps;
        }

        /// <summary>
        /// Create a folder for each distinct application
        /// </summary>
        public void CreateFoldersCompany()
        {
            foreach (cp_Application j in GetJobApplications())
            {
                DirectoryInfo directory = new DirectoryInfo(RootPath + j.CompanyName);
                if (!directory.Exists)
                {
                    DirectoryInfo dir = new DirectoryInfo(RootPath + j.CompanyName);
                    dir.Create();
                }
            }
        }

        public void GetAllFiles()
        {
            //Get the file name associated with each application
            DirectoryInfo di = new DirectoryInfo(RootPath);
            GetJobApplications();

            //Copy the file in the folder with the same name as the application company name
            foreach (cp_Application ja in GetJobApplications())
            {
                FileInfo fi = new FileInfo(RootPath + ja.FilePath);
                if (fi.Exists)
                {
                    fi.CopyTo(RootPath + ja.CompanyName + @"\" + ja.FilePath, true);
                }
                FileInfo fil = new FileInfo(RootPath + ja.AdPath);
                if (fil.Exists)
                {
                    fil.CopyTo(RootPath + ja.CompanyName + @"\" + ja.AdPath, true);
                }
            }
        }

    }   
    }
